<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item :to="{ path: '/market/marketingPlay' }"> <i class="el-icon-lx-cascades"></i> 营销玩法
                </el-breadcrumb-item>
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 邀请码 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="extend_tabs">
            <el-tabs v-model="activeName" @tab-click="handleClick" type="border-card">
                <el-tab-pane label="课程卡管理" name="first">
                    <div class="container">
                        <el-button type="primary" class="handle-del mr10" @click="addDialog">创建邀请码</el-button>
                        <div class="handle-box">
                            <el-input v-model="query.keyword" placeholder="输入卡片名称" class="handle-input mr10"></el-input>
                            <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                        </div>
                        <el-table :data="tableData" class="table" ref="multipleTable"
                            header-cell-class-name="table-header">
                            <el-table-column prop="title" label="邀请码标题"></el-table-column>
                            <el-table-column prop="num" label="激活码数量"></el-table-column>
                            <el-table-column prop="prefix" label="激活码前缀"></el-table-column>
                            <el-table-column label="授权课程" width="300">
                                <template slot-scope="scope">
                                    <div class="course-info-wrapper">
                                        <div class="img-wrapper">
                                            <img :src="scope.row.info.cover" alt="" class="info-image" />
                                        </div>
                                        <div class="info-detail">
                                            <div class="title">
                                                <span>{{ scope.row.info.title }}</span>
                                            </div>
                                            <div class="price">{{ scope.row.info.price == 0 ? '免费' : '¥ ' +
                                                    scope.row.info.price
                                            }}</div>
                                        </div>
                                    </div>
                                </template>
                            </el-table-column>
                            <el-table-column label="状态">
                                <template slot-scope="scope">
                                    {{ scope.row.status == 0 ? '禁用' : '启用' }}
                                </template>
                            </el-table-column>
                            <el-table-column prop="create_time" label="创建时间"></el-table-column>
                            <el-table-column label="操作" width="300" align="center">
                                <template slot-scope="scope">
                                    <el-row>
                                        <el-button @click="activationCode(scope.row.id, scope.row.info_type)"
                                            type="primary" size="mini">激活码</el-button>
                                        <el-button @click="edit(scope.row.id, scope.row)" type="success" size="mini">编辑
                                        </el-button>
                                        <el-button @click="disable(scope.row.id, scope.row.status)" type="warning"
                                            size="mini">{{
                                                    scope.row.status == 0 ? '启用' : '禁用'
                                            }}</el-button>
                                        <el-button @click="deleteRow(scope.row.id, scope.$index)" type="danger"
                                            size="mini">删除</el-button>
                                    </el-row>
                                </template>
                            </el-table-column>
                        </el-table>
                        <div class="pagination">
                            <el-pagination background layout="total, prev, pager, next" :current-page="query.page"
                                :page-size="query.rows" :total="cardListTotal" @current-change="handlePageChange">
                            </el-pagination>
                        </div>
                    </div>
                    <!-- 新增课程卡片弹出框 -->
                    <el-dialog title="新增/编辑" :visible.sync="showDialog" width="30%">
                        <el-form ref="form" :model="form" label-width="100px">
                            <el-form-item label="激活卡名称">
                                <el-input v-model="form.title"></el-input>
                            </el-form-item>
                            <el-form-item label="使用说明描述">
                                <el-input type="textarea" :rows="2" v-model="form.description"> </el-input>
                            </el-form-item>
                            <el-form-item label="激活码数量">
                                <el-input v-model="form.num"></el-input>
                            </el-form-item>
                            <el-form-item label="激活码前缀">
                                <el-input v-model="form.prefix"></el-input>
                            </el-form-item>
                            <el-form-item label="有效时间">
                                <template #default="{ row }">
                                    <el-date-picker v-model="form.time" type="daterange" range-separator="至"
                                      value-format="yyyy-MM-dd HH:mm:ss"  start-placeholder="开始日期" end-placeholder="结束日期">
                                    </el-date-picker>
                                </template>
                            </el-form-item>
                            <el-form-item label="授权课程">
                                <el-button @click="chooseCourse" type="primary" size="small">选择课程</el-button>
                                <el-table :data="selectRowData" v-if="selectRowData.length !== 0" class="table"
                                    ref="multipleTable" header-cell-class-name="table-header">
                                    <el-table-column label="商品信息" width="250">
                                        <template slot-scope="scope">
                                            <div class="course-info-wrapper">
                                                <div class="img-wrapper">
                                                    <img :src="scope.row.cover" alt="" class="info-image-mini" />
                                                </div>
                                                <div class="info-detail">
                                                    <div class="title">
                                                        <span>{{ scope.row.title }}</span>
                                                    </div>
                                                    <span>{{ scope.row.description }}</span>
                                                </div>
                                            </div>
                                        </template>
                                    </el-table-column>
                                    <el-table-column label="类型">
                                        <template slot-scope="scope">
                                            <span>{{
                                                    scope.row.type == 'image_text'
                                                        ? '图文'
                                                        : scope.row.type == 'audio'
                                                            ? '音频'
                                                            : scope.row.type == 'video'
                                                                ? '视频'
                                                                : scope.row.type == 'column'
                                                                    ? '专栏'
                                                                    : '试卷'
                                            }}</span>
                                        </template>
                                    </el-table-column>
                                    <el-table-column label="价格">
                                        <template slot-scope="scope">
                                            <span class="red">{{ '¥ ' + scope.row.price }}</span>
                                        </template>
                                    </el-table-column>
                                </el-table>
                            </el-form-item>
                            <!-- <el-form-item label="排序">
                                <el-input v-model="form.sort"></el-input>
                            </el-form-item> -->
                            <el-form-item label="状态">
                                <el-radio-group v-model="form.status">
                                    <el-radio :label="1">启用</el-radio>
                                    <el-radio :label="0">禁用</el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-form>
                        <span slot="footer" class="dialog-footer">
                            <el-button @click="cancel">取 消</el-button>
                            <el-button type="primary" @click="save">确 定</el-button>
                        </span>
                    </el-dialog>
                    <!-- 选择产品类型 -->
                    <el-dialog width="30%" title="选择产品类型" :visible.sync="productTypeVisible" append-to-body>
                        <div class="product_type">
                            <span v-for="item in productType" :key="item.id" @click="chooseType(item.value)">
                                {{ item.label }}
                            </span>
                        </div>
                    </el-dialog>
                    <!-- 打开内层弹框 -->
                    <el-dialog width="36%" :title="`选择${productTypeTitle}`" @close="exit" :visible.sync="innerVisible"
                        append-to-body>
                        <el-form ref="form" :model="form" label-width="100px">
                            <el-form-item>
                                <el-input v-model="query.keyword" :placeholder="`输入${productTypeTitle}名称`"
                                    class="handle-input mr10"></el-input>
                                <el-button type="primary" icon="el-icon-search" @click="courseSearch">搜索</el-button>
                            </el-form-item>
                            <el-table :data="chooseCourseTableData" @row-click="chooseRow" class="chooseCourseTable"
                                ref="multipleTable" header-cell-class-name="table-header">
                                <el-table-column :label="`${productTypeTitle}封面`">
                                    <template slot-scope="scope">
                                        <img :src="scope.row.cover" alt="" class="info-image" />
                                    </template>
                                </el-table-column>
                                <el-table-column prop="title" :label="`${productTypeTitle}名称`"></el-table-column>
                                <el-table-column label="免费/付费">
                                    <template slot-scope="scope">
                                        <span style="color: #e65c5c">{{ scope.row.price == 0 ? '免费' : '¥ ' +
                                                scope.row.price
                                        }}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="type" :label="`${productTypeTitle}类型`"
                                    v-if="productTypeTitle == '课程'">
                                    <template slot-scope="scope">
                                        {{ scope.row.type == 'video' ? '视频' : scope.row.type == 'audio' ? '音频' : '图文' }}
                                    </template>
                                </el-table-column>
                                <el-table-column prop="status" label="状态">
                                    <template slot-scope="scope">
                                        <span v-if="productTypeTitle == '试卷'">{{ scope.row.status == 0 ? '禁用' : '启用'
                                        }}</span>
                                        <span v-else>{{ scope.row.status == 0 ? '未上架' : '已上架' }}</span>
                                    </template>
                                </el-table-column>
                            </el-table>
                            <div class="pagination">
                                <el-pagination background layout="total, prev, pager, next" :current-page="query.page"
                                    :page-size="query.rows" :total="courseListTotal" @current-change="coursePageChange">
                                </el-pagination>
                            </div>
                        </el-form>

                        <span slot="footer" class="dialog-footer">
                            <el-button type="primary" @click="exit">关 闭</el-button>
                        </span>
                    </el-dialog>
                </el-tab-pane>

                <el-tab-pane label="会员卡管理" name="second">
                    <div class="container">
                        <el-button type="primary" class="handle-del mr10" @click="addMemberCardDialog">新增会员卡片
                        </el-button>
                        <div class="handle-box">
                            <el-input v-model="query.keyword" placeholder="输入卡片姓名" class="handle-input mr10"></el-input>
                            <el-button type="primary" icon="el-icon-search" @click="memberCardSearch">搜索</el-button>
                        </div>
                        <el-table :data="memberCardData" class="table" ref="multipleTable"
                            header-cell-class-name="table-header">
                            <el-table-column prop="title" label="卡片名称"></el-table-column>
                            <el-table-column prop="num" label="激活码数量"></el-table-column>
                            <el-table-column prop="prefix" label="激活码前缀"></el-table-column>
                            <el-table-column prop="cycle_str" label="会员卡/周期"></el-table-column>
                            <el-table-column prop="title" label="状态">
                                <template slot-scope="scope">{{ scope.row.status == 0 ? '禁用' : '启用' }}</template>
                            </el-table-column>
                            <el-table-column prop="create_time" label="创建时间"></el-table-column>
                            <el-table-column label="操作" width="300" align="center">
                                <template slot-scope="scope">
                                    <el-row>
                                        <el-button @click="activationCode(scope.row.id, scope.row.info_type)"
                                            type="primary" size="mini">激活码</el-button>
                                        <el-button @click="memberCardEdit(scope.row.id, scope.row)" type="success"
                                            size="mini">编辑</el-button>
                                        <el-button @click="memberCardDisable(scope.row.id, scope.row.status)"
                                            type="warning" size="mini">{{
                                                    scope.row.status == 0 ? '启用' : '禁用'
                                            }}</el-button>
                                        <el-button @click="memberCardDeleteRow(scope.row.id, scope.$index)"
                                            type="danger" size="mini">删除</el-button>
                                    </el-row>
                                </template>
                            </el-table-column>
                        </el-table>
                        <div class="pagination">
                            <el-pagination background layout="total, prev, pager, next" :current-page="query.page"
                                :page-size="query.rows" :total="memberCardPageTotal"
                                @current-change="memberCardPageChange"></el-pagination>
                        </div>
                    </div>
                    <!-- 新增会员卡片弹出框 -->
                    <el-dialog title="新增/编辑" :visible.sync="memberCardDialog" width="30%">
                        <el-form ref="form" :model="memberCardForm" label-width="100px">
                            <el-form-item label="激活卡名称">
                                <el-input v-model="memberCardForm.title"></el-input>
                            </el-form-item>
                            <el-form-item label="使用说明描述">
                                <el-input type="textarea" :rows="2" v-model="memberCardForm.description"></el-input>
                            </el-form-item>
                            <el-form-item label="激活码数量">
                                <el-input v-model="memberCardForm.num"></el-input>
                            </el-form-item>
                            <el-form-item label="激活码前缀">
                                <el-input v-model="memberCardForm.prefix"></el-input>
                            </el-form-item>
                            <el-form-item label="会员类型">
                                <el-select v-model="memberCardForm.info_id" clearable placeholder="请选择会员类型">
                                    <el-option v-for="item in options" :key="item.id" :label="item.title"
                                        :value="item.id"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="开通时间">
                                <el-select v-model="memberCardForm.cycle" placeholder="请选择开通时间" clearable>
                                    <el-option key="1" label="月" value="month"></el-option>
                                    <el-option key="2" label="季" value="quarter"></el-option>
                                    <el-option key="3" label="年" value="year"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="排序">
                                <el-input v-model="memberCardForm.sort"></el-input>
                            </el-form-item>
                            <el-form-item label="状态">
                                <el-radio-group v-model="memberCardForm.status">
                                    <el-radio :label="1">启用</el-radio>
                                    <el-radio :label="0">禁用</el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-form>
                        <span slot="footer" class="dialog-footer">
                            <el-button @click="memberCardCancel">取 消</el-button>
                            <el-button type="primary" @click="memberCardSave">确 定</el-button>
                        </span>
                    </el-dialog>
                </el-tab-pane>
            </el-tabs>
        </div>
    </div>
</template>

<script>
import {
    courseCardListData,
    videoListData,
    columnListData,
    examListData,
    addCourseCardData,
    delCourseCardData,
    memberCardListData,
    memberSetListData,
    addCardData,
    delCardData
} from '../../api/index';
export default {
    // name: 'inviteCode',
    data() {
        return {
            activeName: 'first',
            query: {
                page: 1,
                rows: 4,
                keyword: ''
            },
            form: {
                id: '',
                info_id: '',
                info_type: '',
                title: '',
                description: '',
                num: '',
                prefix: '',
                // sort: '',
                status: '',
                time:""
            },
            productType: [
                {
                    value: 'knowledge',
                    label: '图文'
                },
                {
                    value: 'knowledge',
                    label: '音频'
                },
                {
                    value: 'knowledge',
                    label: '视频'
                },
                {
                    value: 'column',
                    label: '专栏'
                },
                {
                    value: 'ExamPaper',
                    label: '试卷'
                }
            ],
            memberCardForm: {
                id: '',
                info_type: 'member',
                title: '',
                description: '',
                num: '',
                prefix: '',
                info_id: '',
                cycle: '',
                sort: '',
                status: ''
            },
            tableData: [],
            chooseCourseTableData: [],
            selectRowData: [],
            productTypeTitle: '',
            showDialog: false,
            productTypeVisible: false,
            innerVisible: false,
            cardListTotal: 0,
            courseListTotal: 0,
            memberCardListTotal: 0,
            // 会员卡
            memberCardData: [],
            options: [],
            memberCardPageTotal: 0,
            memberCardDialog: false
        };
    },
    created() {
        this.getData();
        this.getMemberCardData();
    },
    mounted() {
        this.tableLocal();
    },
    methods: {
        handleClick(tab) {
            localStorage.setItem('member_card', tab.name);
        },
        tableLocal() {
            if (localStorage.getItem('member_card') !== null) {
                this.activeName = localStorage.getItem('member_card');
            }
        },
        getData() {
            courseCardListData(this.query).then((res) => {
                this.tableData = res.data.list;
                this.cardListTotal = res.data.count;
            });
        },
        getMemberCardData() {
            memberCardListData(this.query).then((res) => {
                this.memberCardData = res.data.list;
                this.memberCardPageTotal = res.data.count;
            });
            memberSetListData().then((res) => {
                this.options = res.data.list;
            });
        },
        // 搜索
        handleSearch() {
            this.$set(this.query);
            this.getData();
        },
        // 课程搜索
        courseSearch() {
            if (this.productTypeTitle == '课程') {
                videoListData(this.query).then((res) => {
                    this.chooseCourseTableData = res.data.list;
                    this.courseListTotal = res.data.count;
                });
            } else if (this.productTypeTitle == '专栏') {
                columnListData(this.query).then((res) => {
                    this.chooseCourseTableData = res.data.list;
                    this.courseListTotal = res.data.count;
                });
            } else {
                examListData(this.query).then((res) => {
                    this.chooseCourseTableData = res.data.list;
                    this.courseListTotal = res.data.count;
                });
            }
        },
        chooseCourse() {
            this.productTypeVisible = true;
        },
        chooseType(type) {
            this.innerVisible = true;
            this.query.page = 1;
            this.query.keyword = '';
            if (type == 'knowledge') {
                this.productTypeTitle = '课程';
                videoListData(this.query).then((res) => {
                    this.chooseCourseTableData = res.data.list;
                    this.courseListTotal = res.data.count;
                });
            } else if (type == 'column') {
                this.productTypeTitle = '专栏';
                columnListData(this.query).then((res) => {
                    this.chooseCourseTableData = res.data.list;
                    this.courseListTotal = res.data.count;
                });
            } else {
                this.productTypeTitle = '试卷';
                examListData(this.query).then((res) => {
                    this.chooseCourseTableData = res.data.list;
                    this.courseListTotal = res.data.count;
                });
            }
        },
        chooseRow(row) {
            this.selectRowData = [];
            this.selectRowData.push(row);
            this.form.info_id = row.id;
            if (row.type == 'image_text' || row.type == 'audio' || row.type == 'video') {
                this.form.info_type = 'knowledge';
            } else if (row.type == 'column') {
                this.form.info_type = 'column';
            } else {
                this.form.info_type = 'ExamPaper';
            }

            this.innerVisible = false;
            this.productTypeVisible = false;
        },
        //新增
        addDialog() {
            this.showDialog = true;
            this.selectRowData = [];
        },
        // 激活码
        activationCode(id, info_type) {
            this.$router.push({ path: '/activation_code', query: { id, info_type } });
        },
        //编辑
        edit(id, row) {
            this.selectRowData = [];
            this.showDialog = true;
            this.form.id = id;
            this.form = row;
            this.selectRowData.push(row.info);
        },
        // 禁用
        disable(id, status) {
            this.$confirm(`确定${status == 0 ? '启用' : '禁用'}吗！`, '提示', {
                type: 'warning'
            }).then(() => {
                addCourseCardData({ id: id, status: status == 0 ? 1 : 0 }).then((res) => {
                    if (res.code == 1) {
                        this.$message.success('操作成功');
                        this.getData();
                    }
                });
            });
        },
        //删除
        deleteRow(id, index) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                delCourseCardData({ id}).then((res) => {
                    if (res.code == 1) {
                        this.$message.success('删除成功');
                        this.tableData.splice(index, 1);
                        // setTimeout(() => {
                        //     location.reload();
                        // }, 200);
                    }
                });
            });
        },
        //取消
        cancel() {
            this.showDialog = false;
            this.form = {};
        },
        //确定
        save() {
            addCourseCardData(this.form).then((res) => {
                if (res.code == 1) {
                    this.showDialog = false;
                    this.$message.success('操作成功');
                    this.getData();
                    this.getMemberCardData();
                }
            });
        },
        exit() {
            this.innerVisible = false;
        },
        // 分页
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        },
        // 课程分页
        coursePageChange(val) {
            this.$set(this.query, 'page', val);
            this.courseSearch();
        },

        // 会员卡

        // 搜索
        memberCardSearch() {
            this.$set(this.query);
            this.getMemberCardData();
        },
        // 分页
        memberCardPageChange(val) {
            this.$set(this.query, 'page', val);
            this.getMemberCardData();
        },
        //新增
        addMemberCardDialog() {
            this.memberCardDialog = true;
        },
        //取消
        memberCardCancel() {
            this.memberCardDialog = false;
            this.memberCardForm = {};
        },
        //确定
        memberCardSave() {
            addCardData(this.memberCardForm).then((res) => {
                if (res.code == 1) {
                    this.memberCardDialog = false;
                    this.$message.success('操作成功');
                    this.getData();
                }
            });
        },

        //编辑
        memberCardEdit(id, row) {
            this.memberCardDialog = true;
            this.memberCardForm.id = id;
            this.memberCardForm = row;
        },
        //禁用
        memberCardDisable(id, status) {
            this.$confirm(`确定${status == 0 ? '启用' : '禁用'}吗！`, '提示', {
                type: 'warning'
            }).then(() => {
                addCardData({ id: id, status: status == 0 ? 1 : 0 }).then((res) => {
                    if (res.code == 1) {
                        this.$message.success('操作成功');
                        this.getData();
                    }
                });
            });
        },
        //删除
        memberCardDeleteRow(id, index) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                delCardData({ id }).then((res) => {
                    if (res.code == 1) {
                        this.$message.success('删除成功');
                        this.memberCardData.splice(index, 1);
                        // setTimeout(() => {
                        //     location.reload();
                        // }, 200);
                    }
                });
            });
        },
    }
};
</script>

<style scoped>
.extend_tabs {
    padding: 20px;
}

.container {
    border: 0;
}

.handle-box {
    margin-bottom: 20px;
    float: right;
}

.handle-input {
    width: 300px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}

.mr10 {
    margin-right: 10px;
}

.course-info-wrapper {
    display: flex;
    align-items: center;
    width: 100%;
    height: 64px;
    text-align: left;
    cursor: pointer;
}

.course-info-wrapper .img-wrapper {
    position: relative;
}

.info-image {
    height: 64px;
    width: 80px;
    border-radius: 4px;
}

.info-image-mini {
    height: 60px;
    width: 60px;
    border-radius: 50%;
}

.red {
    color: red;
}

.course-info-wrapper .info-detail {
    line-height: 1.5;
    height: 100%;
    margin-left: 8px;
}

.course-info-wrapper .info-detail .title {
    max-width: 100%;
    height: 44px;
    font-size: 14px;
    font-weight: 400;
    color: #333333;
    line-height: 22px;
    overflow: hidden;
    text-overflow: ellipsis;
    word-wrap: break-word;
    white-space: pre-wrap;
}

.course-info-wrapper .info-detail .price {
    font-size: 14px;
    font-weight: 400;
    color: #e65c5c;
}

.user_avatar {
    float: left;
    width: 50px;
    height: 50px;
}

.user_avatar img {
    width: 100%;
    border-radius: 50%;
    overflow: hidden;
}

.user_nickname {
    margin-left: 60px;
    line-height: 50px;
}

.choice-box {
    text-align: center;
    margin-bottom: 20px;
}

.course_list_item {
    float: left;
    width: 180px;
    margin: 10px;
    padding: 5px;
    border: 1px solid #eee;
    cursor: pointer;
    height: 62px;
}

.course_list_item .avatar {
    float: left;
    width: 50px;
    height: 50px;
}

.course_list_item .avatar img {
    width: 100%;
    border-radius: 50%;
}

.course_list_item .nickname {
    margin-left: 60px;
    line-height: 28px;
}

.innerPagination {
    float: left;
}

.dialog-footer {
    display: block;
}

.product_type {
    text-align: center;
}

.product_type span {
    cursor: pointer;
    display: inline-block;
    padding: 0 5px;
    margin: 0 20px;
    color: #fff;
    background: rgba(0, 0, 0, 0.5);
    border-radius: 3px;
}

.chooseCourseTable {
    cursor: pointer;
}
</style>